local rule = fk.CreateSkill {
  name = "#lvshi_rule&",
  mode_skill = true,
}

local U = require "packages/utility/utility"
local ls = require "packages/lvshi/utility"

--获得国策
rule:addEffect(fk.GameStart, {
  priority = 0.001,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player.role == "lord" and #player.room.players > 5
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local lordskills = {}
      for _, name in ipairs(player:getSkillNameList()) do
        if Fk.skills[name]:hasTag(Skill.Lord) then
          table.insertIfNeed(lordskills, name)
        end
      end
      if #lordskills > 0 and player.room:askToSkillInvoke(player, {
        skill_name = "lvshi__policy",
        prompt = "#lvshi__policy-ask"}) then
        for _, name in ipairs(player:getSkillNameList()) do
          if Fk.skills[name]:hasTag(Skill.Lord) then
            room:handleAddLoseSkills(player, "-" .. name, nil, false, true)
            --room:addTableMark(player, MarkEnum.InvalidSkills, s.name)
          end
        end
        local lvshi__policy = room:askToChoice(player, {
          choices = {"lvshi__gulve&", "lvshi__zunwei&", "lvshi__chongwu&"},
          skill_name = "lvshi__policy",
          prompt = "#lvshi__policy-get",
          detailed = true,
        })
        --ls.choicelog(player, lvshi__policy)
        room:setBanner("@[:]lvshi__policy", lvshi__policy)
        room:handleAddLoseSkills(player, lvshi__policy, nil, false)
      end
  end,
})

--回合开始亮身份
rule:addEffect(fk.TurnStart, {
  priority = 1.001,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and not player.role_shown
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = self.name, prompt = "#lvshi_show_role"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerProperty(player, "role_shown", true)
    room:broadcastProperty(player, "role")
    room.logic:trigger(ls.RoleShown, player, {from = player})
    if player.role == "loyalist" then
      room:handleAddLoseSkills(player, "lvshi__zhiyuan&")
      local from = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = table.filter(room.alive_players, function (p) return #p:getCardIds("ej") > 0 end),
        skill_name = rule.name,
        prompt = "#lvshi_role-choose",
        cancelable = true,
      })
      if #from > 0 then
        local cid = room:askToChooseCard(player, {
          target = from[1],
          flag = "ej",
          skill_name = rule.name,
        })
        if cid then
          room:moveCardTo(cid, Card.DiscardPile, nil, fk.ReasonDiscard, rule.name, "", true)
        end
      end
    elseif player.role == "rebel" then
      if not room:getTag("prophet") or room:getTag("prophet") == "" then
        room:setTag("prophet", player.id)
        room:addPlayerMark(player, "@!prophet")
        player:addFakeSkill("prophet_skill&")
        player:prelightSkill("prophet_skill&", true)
        room:setPlayerProperty(player, "role", "rebel_chief")
      else
        local targets = table.filter(room:getOtherPlayers(player),
        function(p) return p:getHandcardNum() <= player:getHandcardNum() and ls.isFriend(player, p) end)
        local to = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = targets,
          skill_name = rule.name,
          prompt = "#lvshi_rebel-choose",
          cancelable = true,
        })
        if #to > 0 then
          to[1]:drawCards(1, rule.name)
        end
      end
    elseif player.role == "renegade" and #room.players > 5 then
      local destiny = room:getTag("destiny")
      if not destiny or destiny == "" then
        destiny = {"lvshi__zhengshi", "lvshi__duotian", "lvshi__zhengmiao", "lvshi__shenye",
        "lvshi__jianbing", "lvshi__kuntu", "lvshi__xuanyin"}
      end
      local n = 6
      if #room.players >= 8 then n = 3 end
      local choice = room:askToChoice(player, {
        choices = table.random(destiny, n),
        skill_name = "lvshi__destiny",
        prompt = "#destiny-get",
        detailed = true,
      })
      U.setPrivateMark(player, "destiny", {choice})
      table.removeOne(destiny, choice)
      room:setTag("destiny", destiny)
      room:handleAddLoseSkills(player, "#lvshi__destiny&", rule.name, false)
    end
  end,
})

Fk:loadTranslationTable{
  ["@[:]lvshi__policy"] = "国策",
  ["lvshi__policy"] = "国策",
  ["#lvshi__policy-ask"] = "你可以用一项国策替换主公技",
  ["#lvshi__policy-get"] = "请选择一项国策获得之",
  ["#lvshi_show_role"] = "你可以明置身份牌",
  ["#lvshi_role-choose"] = "你可以弃置一名角色场上的一张牌",
  ["#lvshi_rebel-choose"] = "你可以令一名手牌不大于你的其他己方角色摸一张牌",
  ["lvshi__destiny"] = "天命",
  ["#destiny-get"] = "请选择一项天命获得之",
}

return rule
